package cn.readjava.controller.user;

import cn.readjava.bean.User;
import cn.readjava.bean.UserOrder;
import cn.readjava.bean.Video;
import cn.readjava.bean.VideoCategory;
import cn.readjava.service.UserOrderService;
import cn.readjava.service.VideoCategoryService;
import cn.readjava.service.VideoService;
import cn.readjava.vo.VideoStatusVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user/video")
public class UserVideoController {

    @Resource
    private VideoCategoryService videoCategoryService;
    @Resource
    private VideoService videoService;
    @Resource
    private UserOrderService userOrderService;

    @GetMapping("/index")
    public String index(Model model) {
        List<VideoStatusVo> videoStatusVoList = new ArrayList<>();
        // 当前用户订单
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        List<UserOrder> userOrderList = userOrderService.findAllByOrderUserId(principal.getUserId());
        // 视频资源
        List<VideoCategory> vcList = videoCategoryService.findAll();

        for (VideoCategory vc : vcList) {
            VideoStatusVo statusVo = new VideoStatusVo();
            if (vc.getVcState() == 0) {
                statusVo.setVideoCategory(vc);
                statusVo.setBuyStatus(100);
                for (UserOrder order : userOrderList) {
                    if (order.getOrderVcId() == vc.getVcId()) {
                        statusVo.setBuyStatus(200);
                    }
                }
            }
            videoStatusVoList.add(statusVo);
        }
        model.addAttribute("videoStatusVoList", videoStatusVoList);
        return "user/video-index";
    }

    @GetMapping("/detail/{id}")
    public String index(@PathVariable Long id, Model model) {
        // 当前用户订单，避免拿到链接强行打开页面
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        List<UserOrder> userOrderList = userOrderService.findAllByOrderUserId(principal.getUserId());
        boolean flag = false;
        for (UserOrder temp : userOrderList) {
            if (temp.getOrderVcId() == id) {
                flag = true;
            }
        }
        if (flag) {
            // 视频资源
            List<Video> vList = videoService.findByTypeId(id);
            List<Video> videoList = new ArrayList<>();
            for (Video video : vList) {
                if (video.getVideoState() == 0) {
                    videoList.add(video);
                }
            }
            model.addAttribute("videoList", videoList);
        }
        return "user/video-detail";
    }
}
